<!doctype html>
<html>
  <head>
    <title>Mocha</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <link rel="stylesheet" href="/node_modules/mocha/mocha.css" />
    <style>
      .headless .suite > h1,
      .headless .test.pass {
        display: none;
      }
    </style>

    <script>
      // Show only errors in "headless", non-interactive mode.
      if (/headless=true/.test(location.href)) {
        document.documentElement.className = 'headless';
      }
    </script>
    <script src="/node_modules/sinon/pkg/sinon.js"></script>
    <script src="/node_modules/chai/chai.js"></script>
    <script src="/node_modules/dirty-chai/lib/dirty-chai.js"></script>
    <script src="/node_modules/mocha/mocha.js"></script>
    <script>
      window.expect = chai.expect;
      mocha.setup('bdd');
    </script>
    <script src="/dist/handlebars.js"></script>
    <script src="/spec/vendor/json2.js"></script>
    <script src="/spec/env/common.js"></script>
    <script>
      var CompilerContext = {
        compile: function(template, options) {
          var templateSpec = handlebarsEnv.precompile(template, options);
          return handlebarsEnv.template(safeEval(templateSpec));
        },
        compileWithPartial: function(template, options) {
          return handlebarsEnv.compile(template, options);
        }
      };

      function safeEval(templateSpec) {
        try {
          var ret;
          eval('ret = ' + templateSpec);
          return ret;
        } catch (err) {
          console.error(templateSpec);
          throw err;
        }
      }
    </script>
    <script src="/tmp/tests.js"></script>
    <script>
      onload = function(){
        mocha.globals(['mochaResults'])
        // The test harness leaks under FF. We should have decent global leak coverage from other tests
        if (!navigator.userAgent.match(/Firefox\/([\d.]+)/)) {
          mocha.checkLeaks();
        }
        var runner = mocha.run();

        //Reporting for saucelabs
        var failedTests = [];
        runner.on('end', function(){
          window.mochaResults = runner.stats;
          window.mochaResults.reports = failedTests;
        });

        runner.on('fail', logFailure);

        function logFailure(test, err){

          var flattenTitles = function(test){
            var titles = [];
            while (test.parent.title){
              titles.push(test.parent.title);
              test = test.parent;
            }
            return titles.reverse();
          };

          failedTests.push({
            name: test.title,
            result: false,
            message: err.message,
            stack: err.stack,
            titles: flattenTitles(test)
          });
        };
      };
    </script>
  </head>
  <body>
    <div id="mocha"></div>
  </body>
</html>
